<?php
/**
 * Created by PhpStorm.
 * User: huyi
 * Date: 2018/1/24
 * Time: 15:01
 * 发布/订阅  消费者
 * 参考地址  :  http://blog.csdn.net/demon3182/article/details/77482725
 */
require_once __DIR__ . './../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$config        = array(
    'localhost',
    5672,
    'huyi',
    'huyi123'
);
$exchange_name = 'logs';
$exchange_type = 'fanout';

//链接
$conn    = new AMQPStreamConnection('localhost', 5672, 'huyi', 'huyi123');
$channel = $conn->channel();
//声明一个交换机
$channel->exchange_declare($exchange_name, $exchange_type, false, false, false);

//声明一个随机对列名
list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);
//队列绑定交换机
$channel->queue_bind($queue_name, $exchange_name);

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

$callback = function ($msg){
    echo ' [x] ', $msg->body, "\n";
};
//开始消费
$channel->basic_consume($queue_name, '', false, true, false, false, $callback);

while(count($channel->callbacks)){
    $channel->wait();
}

$channel->close();
$conn->close();
